<?php  

if($_GET['page'] == 'profile' && $_SERVER['REQUEST_METHOD'] == 'POST' && $_GET['edit'] == 'true')
{
	$email = $_POST['email'];
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$bio = $_POST['bio'];
	$aid = $_SESSION['uid'];

	if(empty($email) || empty($fname) || empty($lname) || empty($bio))
	{
		redirect("?page=profile&edit&error=Please fill in all the fields.");
		$error = 1;
	}

	if(filter_var($email,FILTER_VALIDATE_EMAIL) === false)
	{
		redirect("?page=profile&edit&error=Please fill in an valid E-mail address.");
		$error = 1;
	}

	if(!isset($error))
	{
		mysql_query("UPDATE account SET firstname='$fname', lastname='$lname', email='$email', bio='$bio' WHERE id='$aid'")or die(alert(mysql_error()));
		redirect("?page=profile");
	}
}

function getUserProfile()
{
	$cid = $_SESSION['cid'];

	if(!isset($_GET['id']) && isset($_SESSION['user']))
	{
	  $q = mysql_query("SELECT * FROM account WHERE username ='".$_SESSION['user']."' ");
	  $f = mysql_fetch_array($q);

		$q2 = mysql_query("SELECT * FROM characters WHERE id ='".$_SESSION['cid']."' ");
		$f2 = mysql_fetch_array($q2);

			$q3 = mysql_query("SELECT *, (characters.wins/characters.losses) AS n FROM account, characters WHERE characters.aid = account.id");
			$f3 = mysql_fetch_array($q3);

						$wins = $f3['wins'];
						$losses = $f3['losses'];

						if($losses == 0)
						{
							if($wins == 0)
							{
								$kd = -0;
							}
							else
							{
								$kd = $wins;
							}
						}
						else if($wins == 0)
						{
							if($losses == 0)
							{
								$kd = 0;
							}
							else
							{
								$kd = 1/$losses;
							}
						}
						else
						{
							$kd = $wins/$losses;
						}
					
			echo '<div class="profileleft">
					<img src="img/characters/'.getCharPic($f['id']).'" class="charpic width150"><br>
					<div class="profilename">'.$f2['name'].'</div>
				</div>';
			echo '<table class="profiletable">';
			echo '<tr>
					<tr><td width="150"><b>Username</b><td width="370">'.$f['username'].'</td></tr>';
			
					//if isset GET edit, verander naar inputs + save button
					if(!isset($_GET['edit']))
					{
						echo '
						<tr><td><b>E-mail</b><td>'.$f['email'].'</td></tr>
						<tr><td><b>Firstname</b><td>'.$f['firstname'].'</td></tr>
						<tr><td><b>Lastname</b><td>'.$f['lastname'].'</td></tr>
						<tr><td><br></td></tr><tr><td></td>
						<a href="?page=profile&edit"><button id="pbutton" class="bsexy">Edit</button></a>
						
						</tr>';
						
					} 
					
					else 
					{
						echo '
						<form method="post" action="?page=profile&edit=true">
						<div class="perror" style="color:red; float:left;">'.$_GET['error'].'</div>
						<tr><td><b>E-mail</b><td><input type="text" value="'.$f['email'].'" name="email"></td></tr>
						<tr><td><b>Firstname</b><td><input type="text" value=" '.$f['firstname'].'" name="fname"></td></tr>
						<tr><td><b>Lastname</b><td><input type="text" value=" '.$f['lastname'].'" name="lname"></td></tr>
						<tr><td></td><input type="submit" id="pbutton" class="bsexy update" value="Update"></tr>
						';
						
					}
					
					 
			


			echo '
					<tr><td><b>Level</b><td>'.$f2['level'].'</td></tr>
					<tr><td><b>Exp</b><td>'.$f2['xp'].'</td></tr>
					<tr><td><b>Pollars</b><td>'.(getPollars($f['id'])+getPankPollars($f['id'])).'</td></tr>
					<tr><td><b>MaxHP</b><td>'.$f2['max hp'].'</td></tr>
					<tr><td><b>MaxEnergy</b><td>'.$f2['max energy'].'</td></tr>
					<tr><td><b>Attack</b><td>'.getCharTotalAttack($f2['id']).'</td></tr>
					<tr><td><b>Defence</b><td>'.getCharTotalDefence($f2['id']).'</td></tr>
					<tr><td><b>K/D Ratio</b><td>'.round($kd,2).'</td></tr>
					<tr><td><b>Wins</b><td>'.$f2['wins'].'</td></tr>
					<tr><td><b>Losses</b><td>'.$f2['losses'].'</td></tr>
					<tr><td><b>Missions</b><td>'.$f2['finished_missions'].'</td></tr>
					<tr><td>&nbsp;</td></tr>';

					$f['bio'] = str_replace('\"', '', $f['bio']);
					$f['bio'] = str_replace('alt=', '', $f['bio']);

					if(!isset($_GET['edit']))
					{
						echo '<tr><td colspan="2" class="bio">'.$f['bio'].'</td></tr>';
					}
					else
					{
						echo '<tr><td colspan="2"><textarea name="bio">'.$f['bio'].'</textarea></td></tr></form>';
						echo '<script type="text/javascript">CKEDITOR.replace("bio", { width: "520px" });</script>';
					}
					

					echo '<tr><td><b></b></td></tr>
					</table>
					';
	}
	else if(!isset($_GET['id']) && !isset($_SESSION['user']))
	{
		alert('You must be logged in to watch this page.');
		redirect('index.php');
	}
	else
	{
		if($_GET['id'] == $_SESSION['cid'])
		{
			redirect("?page=profile");
		}

		$q2 = mysql_query("SELECT * FROM characters WHERE id ='".$_GET['id']."' ");
		$f2 = mysql_fetch_array($q2);
			
		if(mysql_num_rows($q2) == 1)
		{
			$q = mysql_query("SELECT * FROM account WHERE id ='".$f2['aid']."' ");
		  	$f = mysql_fetch_array($q);


				$q3 = mysql_query("SELECT *, (characters.wins/characters.losses) AS n FROM account, characters WHERE characters.aid = account.id");
				$f3 = mysql_fetch_array($q3);
						
							$wins = $f3['wins'];
							$losses = $f3['losses'];

							if($losses == 0)
							{
								if($wins == 0)
								{
									$kd = -0;
								}
								else
								{
									$kd = $wins;
								}
							}
							else if($wins == 0)
							{
								if($losses == 0)
								{
									$kd = 0;
								}
								else
								{
									$kd = 1/$losses;
								}
							}
							else
							{
								$kd = $wins/$losses;
							}
						
					echo '<div class="profileleft">
							<img src="img/characters/'.getCharPic($f['id']).'" class="charpic width150"><br>
							<div class="profilename">'.$f2['name'].'</div>

							<a href="?page=battle&id='.$f2['id'].'"><button class="bsexy">Attack</button><br>
							<a href="?page=messages&new&receiver='.$f2['name'].'"><button class="bsexy">Message</button></a><br>
							<a href="?page=transactions&name='.$f2['name'].'"><button class="bsexy">Gift</button><br></a>
						</div>';
					echo'<table class="profiletable">';
					echo'
								<tr><td width="150"><b>Level</b><td>'.$f2['level'].'</td></tr>
								<tr><td><b>Exp</b><td>'.$f2['xp'].'</td></tr>
								<tr><td><b>Pollars</b><td>'.(getPollars($f['id'])+getPankPollars($f['id'])).'</td></tr>
								<tr><td><b>K/D Ratio</b><td>'.round($kd,2).'</td></tr>
								<tr><td><b>Wins</b><td>'.$f2['wins'].'</td></tr>
								<tr><td><b>Losses</b><td>'.$f2['losses'].'</td></tr>
								<tr><td><b>MaxHP</b><td>'.$f2['max hp'].'</td></tr>
								<tr><td><b>MaxEnergy</b><td>'.$f2['max energy'].'</td></tr>
								<tr><td><b>Missions</b><td>'.$f2['finished_missions'].'</td></tr>
								<tr><td>&nbsp;</td></tr>';
			
								$f['bio'] = str_replace('\"', '', $f['bio']);
								$f['bio'] = str_replace('alt=', '', $f['bio']);

						echo '
								<tr><td colspan="2" class="bio">'.$f['bio'].'</td></tr>
						</table>
								';
			}
			else
			{
				alert("This user does not exist.");
				redirect("?page=profile");
			}

		}



}


?>